package tianziliang;

import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

//
public class DoubleColorBall {
    public static void main(String[] args) {
        int[] winningNumbers = generateWinningNumbers();
        int[] userNumbers = getUserInput();

        System.out.print("中奖号码为：");
        printArray(winningNumbers);
        System.out.print("您投注号码为：");
        printArray(userNumbers);

        int redBallMatch = countRedBallMatch(winningNumbers, userNumbers);
        int blueBallMatch = (winningNumbers[6] == userNumbers[6])? 1 : 0;

        System.out.println("您命中了" + redBallMatch + "个红色球号码，" + blueBallMatch + "个蓝色球号码。");
        String prizeLevel = getPrizeLevel(redBallMatch, blueBallMatch);
        System.out.println("中奖级别为：" + prizeLevel);
    }

    public static int[] generateWinningNumbers() {
        Random random = new Random();
        Set<Integer> redBalls = new HashSet<>();
        while (redBalls.size() < 6) {
            redBalls
                    .add(random.nextInt(33) + 1);
        }
        int[] numbers = new int[7];
        int index = 0;
        for (int ball : redBalls) {
            numbers
                    [index++] = ball;
        }
        numbers
                [6] = random.nextInt(16) + 1;
        return numbers;
    }

    public static int[] getUserInput() {
        Scanner scanner = new Scanner(System.in);
        Set<Integer> redBalls = new HashSet<>();
        int[] numbers = new int[7];

        for (int i = 0; i < 6; i++) {
            int input;
            do {
                System.out.print("请输入红球号码，输入任意一个范围1-33的数字：");
                input
                        = scanner.nextInt();
                if (redBalls.contains(input)) {
                    System.out.println("该红球号码已存在，请重新输入。");
                }
            } while (redBalls.contains(input) || input < 1 || input > 33);
            redBalls
                    .add(input);
            numbers
                    [i] = input;
        }

        System.out.print("请输入蓝球号码，输入任意一个范围1-16的数字：");
        numbers
                [6] = scanner.nextInt();
        while (numbers[6] < 1 || numbers[6] > 16) {
            System.out.println("蓝球号码范围错误，请重新输入：");
            numbers
                    [6] = scanner.nextInt();
        }

        return numbers;
    }

    public static int countRedBallMatch(int[] winning, int[] user) {
        int count = 0;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if (winning[i] == user[j]) {
                    count
                            ++;
                }
            }
        }
        return count;
    }

    public static String getPrizeLevel(int redMatch, int blueMatch) {
        if (redMatch == 6 && blueMatch == 1) {
            return "一等奖";
        } else if (redMatch == 6 && blueMatch == 0) {
            return "二等奖";
        } else if (redMatch == 5 && blueMatch == 1) {
            return "三等奖";
        } else if (redMatch == 5 && blueMatch == 0 || redMatch == 4 && blueMatch == 1) {
            return "四等奖";
        } else if (redMatch == 4 && blueMatch == 0 || redMatch == 3 && blueMatch == 1) {
            return "五等奖";
        } else if (redMatch == 2 && blueMatch == 1 || redMatch == 1 && blueMatch == 1 || redMatch == 0 && blueMatch == 1) {
            return "六等奖";
        } else {
            return "未中奖";
        }
    }

    public static void printArray(int[] array) {
        System.out.print("[");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            if (i < array.length - 1) {
                System.out.print(",");
            }
        }
        System.out.println("]");
    }
}